// pages/index/index.js
Page({
  data: {
    swiperList: [
      { id: 1, image: "https://picsum.photos/750/300?random=10" },
      { id: 2, image: "https://picsum.photos/750/300?random=11" },
      { id: 3, image: "https://picsum.photos/750/300?random=12" }
    ],
    recommendBooks: []
  },
  onLoad() {
    // 获取全局数据中的书籍列表
    const app = getApp();
    this.setData({
      recommendBooks: app.globalData.bookList
    });
  },
  onShow() {
    // 页面显示时刷新播放状态
    this.refreshPlayerStatus();
  },
  refreshPlayerStatus() {
    const app = getApp();
    this.setData({
      playingBook: app.globalData.playingBook,
      playStatus: app.globalData.playStatus
    });
  },
  goToBookDetail(e) {
    const bookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/bookDetail/bookDetail?id=${bookId}`
    });
  },
  togglePlay() {
    const app = getApp();
    if (app.globalData.playStatus === 'playing') {
      app.globalData.audioContext.pause();
    } else if (app.globalData.playingBook) {
      app.globalData.audioContext.play();
    }
    this.refreshPlayerStatus();
  },
  goToPlayer() {
    wx.navigateTo({
      url: '/pages/player/player'
    });
  }
})
    